
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import CookeryRoomStep from "./CookeryRoomStep";
import CookeryController from "../CookeryController";
import CaptureComp from "../../../engine/comps/CaptureComp";
import CookeryManager from "../CookeryManager";
import ClotheIconView from "../../dressroom/view/ClotheIconView";
import RandomHelper from "../../../cfw/tools/RandomHelper";
import TextureRenderUtils from "../../../engine/TextureRenderUtils";
import ModuleManager from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";
import SDKManager from "../../../sdk/SDKManager";
import ItemView from "../../item/ItemView";
import GridLayerListView from "../../../engine/GridLayerListView";
const { ccclass, property } = cc._decorator;

@ccclass
export default class CookeryRoomStep7 extends CookeryRoomStep {

    @property(cc.Sprite)
    star: cc.Sprite = null;

    @property(CaptureComp)
    capture: CaptureComp = null;

    @property(GridLayerListView)
    scrollView: GridLayerListView = null;

    @property(cc.Prefab)
    prefab: cc.Prefab = null;

    protected controller: CookeryController;

    protected model: CookeryManager;

    @property(cc.Node)
    toyPos: cc.Node = null;

    onButtonNextClick() {
        if (SDKManager.getChannel().cantUseImageDataURL()) {
            this.controller.setTextureInfo(this.capture.getTextureInfo())
        } else {
            this.controller.setDataUrl(this.capture.getDataUrl())
        }

        this.finish()
    }
    finish() {
        cc.log(' finish == ')
        GlobalEvent.instance().emit(EventName.UPDATE_COOKIE_STEP, 7)
    }


    start() {
        this.controller = CookeryController.instance();
        this.model = CookeryManager.instance();
        this.node.active = true;
        let list = this.model.getList()
        this.scrollView.totalCount = list.length
        GlobalEvent.instance().on(EventName.SELECT_COLUMN, this.onButtonColumnClick, this)
        if (SDKManager.getChannel().cantUseImageDataURL()) {
            this.star.node.scaleY = -1;
            cc.log(' start  ===   hello world  ')
            this.star.spriteFrame = TextureRenderUtils.getSpriteFrameByTextureInfo(this.controller.getTextureInfo())
        } else {
            TextureRenderUtils.getSpriteFrameByDataUrl(this.controller.getDataUrl(), (sf: cc.SpriteFrame) => {
                if (cc.isValid(this.node))
                    this.star.spriteFrame = sf
            });
        }
        // 


    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.SELECT_COLUMN, this.onButtonColumnClick, this)
    }


    updateItem(listView: GridLayerListView, pos: number, item: cc.Node) {
        let columnList = this.model.getList()
        let comp: ItemView = this.controller.initView(item, 'ItemView', columnList[pos]) as ItemView
        // if (comp.isLoad()) {
        comp.refresh()
        // } else {

        // }
    }

    onButtonColumnClick(model) {
        let xrange = [-20, 20]
        let yrange = [-20, 20]
        let x = RandomHelper.random(xrange[0], xrange[1])
        let y = RandomHelper.random(yrange[0], yrange[1])
        let node = new cc.Node();
        let sprite = node.addComponent(cc.Sprite)
        node.x = x;
        node.y = y;
        this.toyPos.addChild(node)
        ModuleManager.loadRes(model.getIcon(), ResType.SpriteFrame, (err, item: ResItem) => {
            sprite.spriteFrame = item.getRes();
        })

    }




}
